package org.yxj.bulletinlib;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;
import java.util.ArrayList;
import java.util.List;

public class BulletinView extends ViewFlipper {

    /**
     * 进入动画
     */
    private int inAnim = R.anim.anim_in;
    /**
     * 移出动画
     */
    private int outAnim = R.anim.anim_out;
    /**
     * 切换的间隔时间
     */
    private int flipInterval = 3000;
    /**
     * 实现滚动适配器
     */
    private BulletinAdapter adapter ;

    /**
     * 所有view
     */
    private List<View> views = new ArrayList<>() ;


    public BulletinView(Context context) {
        this(context,null);
    }

    public BulletinView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTypedArray(attrs);
        initConfig();
    }

    /**
     *  属 性
     *
     * @param attrs
     */
    private void initTypedArray(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs,R.styleable.BulletinView);
        int count = typedArray.getIndexCount();
        for (int i = 0; i <count ; i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.BulletinView_flipInterval) {
                flipInterval = typedArray.getInt(attr,3000);
            } else if (attr == R.styleable.BulletinView_inAnim){
                inAnim = typedArray.getResourceId(attr,0);
            } else if (attr == R.styleable.BulletinView_outAnim){
                outAnim = typedArray.getResourceId(attr,0);
            }
        }
        typedArray.recycle();
    }


    /**
     * 配置
     */
    private void initConfig() {
        setFlipInterval(flipInterval);
        setInAnimation(AnimationUtils.loadAnimation(getContext(),inAnim));
        setOutAnimation(AnimationUtils.loadAnimation(getContext(),outAnim));
        this.getInAnimation()
                .setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.v("======onAnimat========","start");
                View lastView = getCurrentView();
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                Log.v("======onAnimat========","end");
                View currentview = getCurrentView();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    /**
     * @param adapter
     */
    public void setAdapter(BulletinAdapter adapter) {
        this.adapter = adapter;
        if (adapter!=null){
            init();
        }
    }


    /**
     *
     */
    private void init() {
        removeAllViews();
        List list = adapter.getList();
        if (list!=null&&list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                final Object obj = list.get(i);
                View view = LayoutInflater.from(getContext()).inflate(adapter.onLayoutId(),this,false);
                views.add(view);
                addView(view);
                adapter.onBindItemView(view,obj,i);

                view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        itemClick(view,obj);
                    }
                });
            }
            start();
        }
    }


    /**
     *自动播放
     */
    public BulletinView start(){
        if (views.size()>1){
            this.startFlipping();
            setAutoStart(true);
        }else {
            stopFlipping();
            setAutoStart(false);
        }
        return BulletinView.this;
    }


    /**
     * 停止播放
     *
     */
    public BulletinView stop(){
        stopFlipping();
        setAutoStart(false);
        return this ;
    }



    /**
     * 点击事件
     *
     * @param view
     * @param obj
     */
    private void itemClick(View view, Object obj){
        if (adapter.getItemBulletinClickListener()!=null){
            adapter.getItemBulletinClickListener().ItemBulletinClick(view,obj);
        }
    }

}
